package com.TT.service.mysql;

import com.TT.common.REST.TypeOfServiceResponse;
import com.TT.common.utils.DataTablePageUtil;
import com.TT.dao.mysql.UserMapper;
import com.TT.domain.mysql.User;
import com.TT.service.BaseService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * Created by 苏文广 on 2018/11/26
 */
@Service("userService")
@Slf4j
public class UserService extends BaseService {
    @Resource
    private UserMapper userMapper;

    /**
     * 注册用户
     *
     * @param user 用户信息，用户名和密码不为空
     */
    public boolean register(User user) throws Exception {
        try {
            userMapper.insertSelective(user);
            return TypeOfServiceResponse.BOOLEAN_SUCCESS;
        } catch (Exception e) {
            tryLog(e);
            throw e;
        }
    }

    /**
     * 登陆
     * @param user 用户信息
     * @return User 用户信息
     * @throws Exception  Exception
     */
    public User login(User user) throws Exception {
        try {
            User user1 = userMapper.selectByUserName(user.getUserName());
            if (ObjectUtils.anyNotNull(user1)) {
                boolean equals = user1.getPassword().equals(user.getPassword());
                if (equals) {
                    return user1;
                }
            }
            return null;
        } catch (Exception e) {
            tryLog(e);
            throw e;
        }

    }

    /**
     *  更新用户信息
     * @param user 用户
     * @throws Exception 异常
     */
    public void updateInfo(User user) throws Exception {
        try {
            userMapper.insertSelective(user);
        } catch (Exception e) {
            tryLog(e);
            throw e;
        }
    }

    public ArrayList<User> getUserList() {
        return userMapper.selectAllOrderByUserName();
    }

    public User getUserInfo(int id) {
        return userMapper.selectByPrimaryKey((long) id);
    }

    public void updateByAdmin(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    public void deleteByUserId(long id) {
        userMapper.deleteByPrimaryKey(id);
    }
}
